package com.hzdl.car.contorlle;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Car;

import com.hzdl.car.bean.Customer;
import com.hzdl.car.bean.Rent;
import com.hzdl.car.service.CarService;

import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.vo.CarVo;

import com.hzdl.car.vo.CustomerVo;
import com.hzdl.car.vo.RentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resources;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("car")
public class CarControlle {
    @Autowired
    private CarService service;
    @RequestMapping("init")
    public HzdlResultJson init(CarVo carVo){
        PageInfo<Car> pageInfo = service.findWhere(carVo);
        return new HzdlResultJson(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("add")
    public HzdlResultJson add(CarVo carVo){


        int row = service.add(carVo);
        if (row>0){
            return new HzdlResultJson("添加成功",null);
        }else {
            return new HzdlResultJson(-1,"添加失败");
        }
    }
    @RequestMapping("update")
    public HzdlResultJson update(CarVo carVo){
        int row = service.update(carVo);
        if (row>0){
            return new HzdlResultJson("修改成功",null);
        }
        return new HzdlResultJson(-1,"修改失败");

    }

    @RequestMapping("delete")
    public HzdlResultJson delete (CarVo carVo){
        int row = service.delete(carVo.getCarnumber());
        if (row>0){
            return HzdlResultJson.DELETE_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_FAIL();
        }
    }

    @RequestMapping("deleteAll")
    public HzdlResultJson deleteAll ( CarVo carVo,@RequestParam(value = "ids") String ids){
        int row =0;
        String[] split = ids.split(",");

        for(String  id:split){
            row+=service.delete(id);
        }

        if (row>0){
            return HzdlResultJson.DELETE_ALL_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_ALL_FAIL();
        }
    }

    @RequestMapping("upload")
    public Map<String,Object> upload(MultipartFile file, HttpSession session){
        String originalFilename = file.getOriginalFilename();
        String path = null;
        Map<String,Object> map = new HashMap<>();
        try {

            path = ResourceUtils.getURL("classpath:").getPath()+"static/images/"+originalFilename;
            File file1=new File(path);
            if (!file1.getParentFile().exists()){
                file1.getParentFile().mkdirs();
            }
            file.transferTo(file1);
            map.put("code", 0);	//0表示上传成功
            map.put("msg","上传成功"); //提示消息
            Map<String,Object> map2 = new HashMap<String,Object>();
            map2.put("src", "/car/static/images/"+originalFilename);
            map2.put("title", originalFilename);
            map.put("data", map2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            map.put("code", -1);	//0表示上传成功
            map.put("msg","上传失败"); //提示消息
            e.printStackTrace();

        }

        return map;
    }

    @RequestMapping("carinit")
    public HzdlResultJson newsInit(CarVo carVo, String identity){
        Customer id = service.findById(identity);
        if (id==null){
            return new  HzdlResultJson(-1,"没有该用户");
        }
        return new HzdlResultJson(null);
    }

    @RequestMapping("chuzu")
    public HzdlResultJson carchuzu(CarVo carVo){
        PageInfo<Car> pageInfo = service.findByisrenting(carVo);
        return new HzdlResultJson(pageInfo.getTotal(),pageInfo.getList());
    }
}
